﻿Imports System.Drawing
Public Class TGridSettings
    Implements System.IDisposable

    Private Const CONFIG_FILE As String = "grid.conf"
    Private _FontList As New List(Of FontFamily)
    Private _HeaderSettings As New TFontSettings
    Private _OddSettings As New TFontSettings
    Private _EvenSettings As New TFontSettings

    Public Sub ApplyAll(ByVal AControl As Control)
        For Each ctrl As Control In AControl.Controls
            If ctrl.GetType Is GetType(System.Windows.Forms.DataGridView) Then
                Apply(ctrl)
            ElseIf ctrl.Controls.Count > 0 Then
                ApplyAll(ctrl)
            End If
        Next
    End Sub

    Public Sub Apply(ByVal AGrid As DataGridView)
        Apply(AGrid.ColumnHeadersDefaultCellStyle, _HeaderSettings)
        Apply(AGrid.RowsDefaultCellStyle, _EvenSettings)
        Apply(AGrid.AlternatingRowsDefaultCellStyle, _OddSettings)
    End Sub

    Private Sub Apply(ByVal ACellStyle As DataGridViewCellStyle, ByVal ASettings As TFontSettings)
        ACellStyle.Font = ASettings.Font
        ACellStyle.ForeColor = ASettings.ForeColor
        ACellStyle.BackColor = ASettings.BackColor
    End Sub
    Public Sub New()
        InitFonts()
        LoadConfig()
    End Sub

    Public Sub LoadConfig()
        LoadConfig("Header", _HeaderSettings)
        LoadConfig("Odd", _OddSettings)
        LoadConfig("Even", _EvenSettings)
    End Sub

    Private Sub LoadConfig(ByVal ASection As String, ByVal AFontSettings As TFontSettings)
        Dim IniFile As New TIniFile(Me.ConfigFile)
        Try
            AFontSettings.FontName = IniFile.ReadString(ASection, "FontName", "MS UI Gothic")
            AFontSettings.FontSize = IniFile.ReadInteger(ASection, "FontSize", 9)
            AFontSettings.FontBold = IniFile.ReadBool(ASection, "Bold", False)
            AFontSettings.FontItalic = IniFile.ReadBool(ASection, "Italic", False)
            AFontSettings.ForeColor = IniFile.ReadColor(ASection, "ForeColor", Color.Black)
            If ASection = "Header" Then
                AFontSettings.BackColor = IniFile.ReadColor(ASection, "BackColor", Color.Gray)
            Else
                AFontSettings.BackColor = IniFile.ReadColor(ASection, "BackColor", Color.White)
            End If
        Finally
            IniFile.Dispose()
        End Try
    End Sub

    Public Sub SaveConfig()
        SaveConfig("Header", _HeaderSettings)
        SaveConfig("Odd", _OddSettings)
        SaveConfig("Even", _EvenSettings)
    End Sub

    Private Sub SaveConfig(ByVal ASection As String, ByVal AFontSettings As TFontSettings)
        Dim IniFile As New TIniFile(Me.ConfigFile)
        Try
            IniFile.WriteString(ASection, "FontName", AFontSettings.FontName)
            IniFile.WriteInteger(ASection, "FontSize", AFontSettings.FontSize)
            IniFile.WriteBool(ASection, "Bold", AFontSettings.FontBold)
            IniFile.WriteBool(ASection, "Italic", AFontSettings.FontItalic)
            IniFile.WriteColor(ASection, "ForeColor", AFontSettings.ForeColor)
            IniFile.WriteColor(ASection, "BackColor", AFontSettings.BackColor)
        Finally
            IniFile.Dispose()
        End Try
    End Sub

    Private ReadOnly Property ConfigFile() As String
        Get
            Return IncludeTrailingPathDelimiter(System.AppDomain.CurrentDomain.BaseDirectory) & CONFIG_FILE
        End Get
    End Property

    Private Function IncludeTrailingPathDelimiter(ByVal APath As String) As String
        If Left(APath, 1) = "\" Then
            Return APath
        Else
            Return APath & "\"
        End If
    End Function

    Public ReadOnly Property HeaderSettings() As TFontSettings
        Get
            Return _HeaderSettings
        End Get
    End Property
    Public ReadOnly Property OddSettings() As TFontSettings
        Get
            Return _OddSettings
        End Get
    End Property
    Public ReadOnly Property EvenSettings() As TFontSettings
        Get
            Return _EvenSettings
        End Get
    End Property

    Public ReadOnly Property FontList() As List(Of FontFamily)
        Get
            Return _FontList
        End Get
    End Property

    Private Sub InitFonts()
        _FontList.Clear()
        Dim ifc As New System.Drawing.Text.InstalledFontCollection
        'インストールされているすべてのフォントファミリアを取得
        Dim ffs As FontFamily() = ifc.Families
        Dim ff As FontFamily
        For Each ff In ffs
            'ここではスタイルにRegularが使用できるフォントのみを表示
            If ff.IsStyleAvailable(FontStyle.Regular) Then
                _FontList.Add(ff)
            End If
        Next ff
    End Sub



    Private disposedValue As Boolean = False        ' 重複する呼び出しを検出するには

    ' IDisposable
    Protected Overridable Sub Dispose(ByVal disposing As Boolean)
        If Not Me.disposedValue Then
            If disposing Then
                ' TODO: 他の状態を解放します (マネージ オブジェクト)。
            End If
            ' TODO: ユーザー独自の状態を解放します (アンマネージ オブジェクト)。
            ' TODO: 大きなフィールドを null に設定します。
        End If
        Me.disposedValue = True
    End Sub

#Region " IDisposable Support "
    ' このコードは、破棄可能なパターンを正しく実装できるように Visual Basic によって追加されました。
    Public Sub Dispose() Implements IDisposable.Dispose
        ' このコードを変更しないでください。クリーンアップ コードを上の Dispose(ByVal disposing As Boolean) に記述します。
        Dispose(True)
        GC.SuppressFinalize(Me)
    End Sub
#End Region

End Class
